package player

import (
	"fmt"
	"game/readygo/socket"
	"tcs-server/tcs-protobuf/src/tcsprotobuf"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"
)

type PlayerContext struct {
	id         int64
	login      bool
	connCtx    *socket.ConnectContext
	serverZone int32
	currMsgId  int32
}

func (ctx PlayerContext) GetId() int64 {
	return ctx.id
}
func (ctx *PlayerContext) SetId(id int64) {
	ctx.id = id
}

func (ctx *PlayerContext) IsLogin() bool {
	return ctx.login
}
func (ctx *PlayerContext) SetLogin(login bool) {
	ctx.login = login
}

func (ctx *PlayerContext) GetConnect() *socket.ConnectContext {
	return ctx.connCtx
}
func (ctx *PlayerContext) SetConnect(connCtx *socket.ConnectContext) {
	ctx.connCtx = connCtx
}

func (ctx *PlayerContext) Write(cmd protoreflect.EnumNumber, message protoreflect.ProtoMessage, messageId int32) {
	ctx.WriteProto(int32(cmd), message, messageId)
}

func (ctx *PlayerContext) WriteProto(cmd int32, message protoreflect.ProtoMessage, messageId int32) {
	messageBytes, err := proto.Marshal(message)
	if err != nil {
		fmt.Println(err)
		return
	}
	var wrapper tcsprotobuf.MessageWrapper
	wrapper.ProtoId = cmd
	wrapper.Data = messageBytes
	wrapperBytes, err := proto.Marshal(&wrapper)
	if err != nil {
		fmt.Println(err)
		return
	}
	ctx.connCtx.ConnectWrite(wrapperBytes)
}

func (ctx *PlayerContext) WriteBytes(cmd int32, message []byte, messageId int32) {
	ctx.connCtx.ConnectWrite(message)
}

func (ctx *PlayerContext) GetServerZone() int32 {
	return ctx.serverZone
}

func (ctx *PlayerContext) GetCurrMsgId() int32 {
	return ctx.currMsgId
}

func (ctx *PlayerContext) SetCurrMsgId(messageId int32) {
	ctx.currMsgId = messageId
}
